from __future__ import division
import jieba
import sys
import pickle
import numpy as np

from sklearn import metrics

import matplotlib.pyplot as plt

from sklearn.metrics import auc


if __name__ == '__main__':
    count_vector_file = sys.argv[1]
    tfidf_file = sys.argv[2]
    lg_file = sys.argv[3]
    test_file = sys.argv[4]
    #
    # count_vector_file = '/Users/hardy/data/gen_models/cv.pkl'
    # tfidf_file = '/Users/hardy/data/gen_models/tfidf.pkl'
    # lg_file = '/Users/hardy/data/gen_models/lg.pkl'

    CV = pickle.load(open(count_vector_file, 'rb'))
    TFIDF = pickle.load(open(tfidf_file, 'rb'))
    LG = pickle.load(open(lg_file, 'rb'))

    correct = 0
    n = 0
    targets = []
    probs = []
    for l in open(test_file):
        t = l.strip()
        d = t.split('\t')
        if len(d) != 3:
            continue

        seg = jieba.lcut(d[1] + d[2])
        target = d[0].strip().replace('__label__', '')
        targets.append(0 if target== 'not_car' else 1)
        c = LG.predict(TFIDF.transform(CV.transform([' '.join(seg)])))[0]
        prob = LG.predict_proba(TFIDF.transform(CV.transform([' '.join(seg)])))[0]
        probs.append(prob[0])

        # print(prob)
        n += 1
        if c == target:
            correct += 1
        else:
            print(target + '\t' + c + '\t' + l)

    fpr, tpr, thresholds = metrics.roc_curve(targets, probs)
    metrics.auc(fpr, tpr)
    print('accuracy: ', correct / n)

